#include <my_header.h>

int main(int argc, char *argv[]){
    ARGS_CHECK(argc,2);
    // 以读写模式打开文件，若文件不存在，没有权限等会打开失败
    int fd = open(argv[1],O_RDWR);
    ERROR_CHECK(fd, -1, "open");

    char buf[4096] = {0};
    ssize_t read_count;

    // 循环读取文件直到文件末尾（sret = 0）或出错(sret = -1)
    while(1){
        read_count = read(fd, buf, sizeof(buf) - 1);
        if(read_count <= 0){
            // 检查是否因为错误推出循环
            ERROR_CHECK(read_count, -1, "read");
            break;
        }

        buf[read_count] = '\0';

        printf("read_count = %ld, buf = %s \n", read_count, buf);
        memset(buf, 0, sizeof(buf));    // 清空缓冲区以准备下一次读取
    }

    close(fd);
    return 0;
}
